<template>
  <div class="page-header-index-wide">
    <a-row :gutter="24">
      <a-col :sm="24" :md="12" :xl="6" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="总销售额" :total="totalSales">
          <a-tooltip title="指标说明" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <trend flag="up" style="margin-right: 16px;">
              <span slot="term">周同比</span>
              {{ weeklyGrowthRate }}
            </trend>
            <trend flag="down">
              <span slot="term">日同比</span>
              {{ dailyGrowthRate }}
            </trend>
          </div>
          <template slot="footer">日均销售额<span>{{ dailyAverageSales }} </span></template>
        </chart-card>
      </a-col>
      <a-col :sm="24" :md="12" :xl="6" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="今日订单量" :total="today_orders_count">
          <a-tooltip title="订单量表示总订单数量，折线图表示近十日订单趋势变化情况" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <mini-area />
          </div>
          <template slot="footer">订单总量<span> {{ total_orders }}</span></template>
        </chart-card>
      </a-col>
      <a-col :sm="24" :md="12" :xl="6" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="处理中订单数" :total="bpm_status_total"><a-tooltip title="指标说明" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <mini-bar :height="40" />
          </div>
          <template slot="footer">处理中任务占比 <span>{{bpm_status_percentage}}</span></template>
         <a-tooltip title="更多" slot="action">
            <a-icon type="ellipsis" @click="bbsHome()"/>
          </a-tooltip>
        </chart-card>
      </a-col>
      <a-col :sm="24" :md="12" :xl="6" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="订单平均满意度" :total="avg_satisfaction">
          <a-tooltip title="满意度平均值" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <mini-progress color="rgb(19, 194, 194)" :target="100" :percentage="avg_tianxingtu" :height="8" />
          </div>
          <template slot="footer">
            <trend  :flag="current_month_value >= 0 ? 'up' : 'down'" style="margin-right: 16px;">
              <span slot="term">本月满意度</span>
              {{ current_month_value }}
            </trend>
            <trend  :flag="previous_month_value >= 0 ? 'up' : 'down'" style="margin-right: 16px;">
              <span slot="term">上月满意度</span>
              {{ previous_month_value }}
            </trend>
            <trend  :flag="month_over_month_growth_percent >= 0 ? 'up' : 'down'">
              <span slot="term">月环比</span>
              {{ month_over_month_growth_percent }}
            </trend>
          </template>
        </chart-card>
      </a-col>
    </a-row>

    <a-card :loading="loading" :bordered="false" :body-style="{padding: '0'}">
      <div class="salesCard">
        <a-tabs default-active-key="1" size="large" :tab-bar-style="{marginBottom: '24px', paddingLeft: '16px'}">
<!--          <div class="extra-wrapper" slot="tabBarExtraContent">
           <div class="extra-item">
              <a>今日</a>
              <a>本周</a>
              <a>本月</a>
              <a>本年</a>
            </div>
            <a-range-picker :style="{width: '256px'}" />
          </div>-->
          <a-tab-pane loading="true" tab="销售额" key="1">
            <a-row>
              <a-col :xl="16" :lg="12" :md="12" :sm="24" :xs="24">
                <bar title="销售额" :dataSource="barData"/>
              </a-col>
              <a-col :xl="8" :lg="12" :md="12" :sm="24" :xs="24">
                <rank-list title="销售额排行榜" :list="rankList"/>
              </a-col>
            </a-row>
          </a-tab-pane>
          <a-tab-pane tab="销售订单" key="2">
            <a-row>
              <a-col :xl="16" :lg="12" :md="12" :sm="24" :xs="24">
                <bar title="销售订单" :dataSource="barData1"/>
              </a-col>
              <a-col :xl="8" :lg="12" :md="12" :sm="24" :xs="24">
                <rank-list title="销售订单排行榜" :list="rankList1"/>
              </a-col>
            </a-row>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-card>

    <a-row>
      <a-col :span="24">
        <a-card :loading="loading" :bordered="false" title="最近一周访问量统计" :style="{ marginTop: '24px' }">
          <a-row>
            <a-col :span="6">
              <head-info title="今日IP" :content="loginfo.todayIp"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="environment" style="font-size: 24px"  />
              </a-spin>
            </a-col>
            <a-col :span="6">
              <head-info title="今日访问" :content="loginfo.todayVisitCount"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="team" style="font-size: 24px"  />
              </a-spin>
            </a-col>
            <a-col :span="6">
              <head-info title="总访问量" :content="loginfo.totalVisitCount"></head-info>
            </a-col>
            <a-col :span="2">
              <a-spin class='circle-cust'>
                <a-icon slot="indicator" type="rise" style="font-size: 24px"  />
              </a-spin>
            </a-col>
          </a-row>
          <line-chart-multid :fields="visitFields" :dataSource="visitInfo"></line-chart-multid>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script>
import { getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ChartCard from '@/components/ChartCard'
  import ACol from "ant-design-vue/es/grid/Col"
  import ATooltip from "ant-design-vue/es/tooltip/Tooltip"
  import MiniArea from '@/components/chart/MiniArea'
  import MiniBar from '@/components/chart/MiniBar'
  import MiniProgress from '@/components/chart/MiniProgress'
  import RankList from '@/components/chart/RankList'
  import Bar from '@/components/chart/Bar'
  import LineChartMultid from '@/components/chart/LineChartMultid'
  import HeadInfo from '@/components/tools/HeadInfo.vue'

  import Trend from '@/components/Trend'
  import { getLoginfo,getVisitInfo } from '@/api/api'

  const rankList = []
  for (let i = 0; i < 7; i++) {
    rankList.push({
      name: '白鹭岛 ' + (i+1) + ' 号店',
      total: 1234.56 - i * 100
    })
  }
  const barData = []
  for (let i = 0; i < 12; i += 1) {
    barData.push({
      x: `${i + 1}月`,
      y: Math.floor(Math.random() * 1000) + 200
    })
  }
const rankList1 = []
for (let i = 0; i < 7; i++) {
  rankList1.push({
    name: '白鹭岛 ' + (i+1) + ' 号店',
    total: 1234.56 - i * 100
  })
}
const barData1 = []
for (let i = 0; i < 12; i += 1) {
  barData.push({
    x: `${i + 1}月`,
    y: Math.floor(Math.random() * 1000) + 200
  })
}
  export default {
    name: "IndexChart",
    components: {
      ATooltip,
      ACol,
      ChartCard,
      MiniArea,
      MiniBar,
      MiniProgress,
      RankList,
      Bar,
      Trend,
      LineChartMultid,
      HeadInfo
    },
    data() {
      return {
        loading: true,
        center: null,
        rankList:[],
        rankList1:[],
        barData,
        barData1:[],
        loginfo:{},
        visitFields:['ip','visit'],
        visitInfo:[],
        indicator: <a-icon type="loading" style="font-size: 24px" spin />,
        totalSales: null,
        weeklyGrowthRate: null,
        dailyGrowthRate: null,
        dailyAverageSales: null,
        total_orders:null,
        today_orders_count:null,
        avg_satisfaction:null,
        current_month_value:null,
        previous_month_value:null,
        month_over_month_growth_percent:null,
        avg_tianxingtu:null,
        bpm_status_total:null,
        bpm_status_percentage:null
      }
    },
    created() {

     /* setTimeout(() => {
        this.loading = !this.loading
      }, 1000)*/
      this.initLogInfo();
      this.fetchData();
      this.getDingdanTotal ();
      this.getDangriDingdanTotal();
      this.getbpmStatusTotal();
      this.getSalesAmountByMonth();
    },
    methods: {
      getDingdanTotal (){
        getAction("/dayincopy/tableDayinCopy/dingdanTotal",'').then(res=>{
          console.log("订单数据"+ JSON.stringify(res.result, null, 2));
          this.total_orders ="" + res.result[0].total_orders;
        }).catch(error => {
          console.error("Error fetching sales data:", error);
        });
      },

      getDangriDingdanTotal(){
        getAction("/dayincopy/tableDayinCopy/dangriDingdanTotal",'').then(res=>{
          console.log("当日订单数据"+ JSON.stringify(res, null, 2));
          this.today_orders_count =res.result;
        }).catch(error => {
          console.error("Error fetching sales data:", error);
        });

      },
      async fetchData() {
            getAction("/dayincopy/tableDayinCopy/total",'').then(res=>{
              this.totalSales = "￥" + res.totalSales;
              this.weeklyGrowthRate = res.weeklyGrowthRate;
              this.dailyGrowthRate = res.dailyGrowthRate;
              this.dailyAverageSales = "￥" + res.dailyAverageSales.toFixed(2);
              this.loading = false;
            }).catch(error => {
              console.error("Error fetching sales data:", error);
              this.loading = false;
            });
        getAction("/dayincopy/tableDingdanHuifang/pingjia",'').then(res=>{
          console.log("评价数据"+ JSON.stringify(res, null, 2));
          this.avg_satisfaction = res.avg_satisfaction +'%';
          this.avg_tianxingtu = res.avg_satisfaction ;
          this.current_month_value = res.current_month_value;
          this.previous_month_value = res.previous_month_value;
          this.month_over_month_growth_percent = res.month_over_month_growth_percent.toFixed(2) +'%';
          this.loading = false;
        }).catch(error => {
          console.error("Error fetching sales data:", error);
          this.loading = false;
        });
        } ,
      async  getSalesAmountByMonth() {
        getAction("/dayincopy/tableDayinCopy/salesAmountByMonth",'').then(res=>{
          const formattedData = res.result.map(e => ({
            y: e.total_amount,
            x: e.name_month
          }));
          if(formattedData.length>0){
            this.barData = formattedData;
          }
          this.loading = false;
        }).catch(error => {
          console.error("Error fetching sales data:", error);
          this.loading = false;
        });
        getAction("/dayincopy/tableDayinCopy/salesOrderMonthDistribution",'').then(res=>{
          console.log("XXXXXXXXXXXXXXXXXX"+ JSON.stringify(res.result, null, 2));
          const formattedData = res.result.map(e => ({
            y: e.total_num,
            x: e.name_month
          }));
          if(formattedData.length>0){
            this.barData1 = formattedData;
          }
          this.loading = false;
        }).catch(error => {
          console.error("Error fetching sales data:", error);
          this.loading = false;
        });

        getAction("/dayincopy/tableDayinCopy/salesAmountRanking",'').then(res=>{
          console.log("salesAmountRanking"+ JSON.stringify(res.result, null, 2));
          const filteredData = res.result.filter(item => item !== null);
          const formattedData = filteredData.map(e => ({
            name: e.dingdan_jiedanyuan,
            total: e.xiaoshou_jine_total
          }));

          if(formattedData.length>0){
            this.rankList = formattedData;
          }
          this.loading = false;
        }).catch(error => {
          console.error("Error fetching sales data:", error);
          this.loading = false;
        });

        getAction("/dayincopy/tableDayinCopy/salesOrderPersonnelRanking",'').then(res=>{
          const filteredData = res.result.filter(item => item !== null);
          const formattedData = filteredData.map(e => ({
            name: e.dingdan_jiedanyuan,
            total: e.xiaoshou_total
          }));
          if(formattedData.length>0){
            this.rankList1 = formattedData;
          }
          this.loading = false;
        }).catch(error => {
          console.error("Error fetching sales data:", error);
          this.loading = false;
        });






      } ,
      getbpmStatusTotal()
      {
        getAction('/dayincopy/tableDayinCopy/bpmStatusTotal', '')
          .then(res => {
            this.bpm_status_total= res.result[1].bpm_status_total;
            this.bpm_status_percentage=res.result[1].bpm_status_percentage;

          })
          .catch(error => {
            console.error('获取订单流程进度:', error)
          })
      },
      initLogInfo () {
        getLoginfo(null).then((res)=>{
          if(res.success){
            Object.keys(res.result).forEach(key=>{
              res.result[key] =res.result[key]+""
            })
            this.loginfo = res.result;
          }
        })
        getVisitInfo().then(res=>{
          if(res.success){
             this.visitInfo = res.result;
           }
         })
      },
    }
  }
</script>

<style lang="less" scoped>
  .circle-cust{
    position: relative;
    top: 28px;
    left: -100%;
  }
  .extra-wrapper {
    line-height: 55px;
    padding-right: 24px;

    .extra-item {
      display: inline-block;
      margin-right: 24px;

      a {
        margin-left: 24px;
      }
    }
  }

  /* 首页访问量统计 */
  .head-info {
    position: relative;
    text-align: left;
    padding: 0 32px 0 0;
    min-width: 125px;

    &.center {
      text-align: center;
      padding: 0 32px;
    }

    span {
      color: rgba(0, 0, 0, .45);
      display: inline-block;
      font-size: .95rem;
      line-height: 42px;
      margin-bottom: 4px;
    }
    p {
      line-height: 42px;
      margin: 0;
      a {
        font-weight: 600;
        font-size: 1rem;
      }
    }
  }
</style>